/*
 * Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君) (herodotus@aliyun.com & www.herodotus.cn)
 *
 * Dante Engine licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * <http://www.gnu.org/licenses/lgpl-3.0.html>
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.herodotus.engine.facility.alibaba.autoconfigure.sentinel;

import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;

/**
 * <p>Description: 基础设施 Sentinel 配置 </p>
 *
 * @author : gengwei.zheng
 * @date : 2022/2/5 17:57
 */
@AutoConfiguration
@ConditionalOnClass
public class FacilityAlibabaSentinelAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(FacilityAlibabaSentinelAutoConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.info("[Herodotus] |- Module [Facility Alibaba Sentinel Starter] Auto Configure.");
    }

//    @Bean
//    @Scope("prototype")
//    @ConditionalOnMissingBean
//    @ConditionalOnProperty(name = "feign.sentinel.enabled")
//    public Feign.Builder feignSentinelBuilder() {
//        return HerodotusSentinelFeign.builder();
//    }
//
//    /**
//     * 限流、熔断统一处理类
//     */
//    @Configuration(proxyBeanMethods = false)
//    @ConditionalOnClass(HttpServletRequest.class)
//    public static class WebmvcHandler {
//        @Bean
//        public BlockExceptionHandler webmvcBlockExceptionHandler() {
//            return (request, response, e) -> {
//                response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
//                Result<String> result = Result.failure("Too many request, please retry later.");
//                response.getWriter().print(Jackson2Utils.toJson(result));
//            };
//        }
//
//    }
//
//    /**
//     * 限流、熔断统一处理类
//     */
//    @Configuration(proxyBeanMethods = false)
//    @ConditionalOnClass(ServerResponse.class)
//    public static class WebfluxHandler {
//        @Bean
//        public BlockRequestHandler webfluxBlockExceptionHandler() {
//            return (exchange, t) ->
//                    ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
//                            .contentType(MediaType.APPLICATION_JSON)
//                            .body(BodyInserters.fromValue(Result.failure(t.getMessage())));
//        }
//    }
}
